package cn.edu.nju.software.cdiy.action.user.all;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.service.Service;
import cn.edu.nju.software.cdiy.util.Constant;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class IndexAction extends ActionSupport implements SessionAware {
	
	private Map<String, Object> session;
	private List<Service<?>> serviceList;

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<Service<?>> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<Service<?>> serviceList) {
		this.serviceList = serviceList;
	}

	@Override
	public String execute() throws Exception {
		if (!session.containsKey(Constant.S_BRAND_LISTS)) {
			List<List<Brand>> brandLists = new LinkedList<List<Brand>>();
			for(Service<?> service : serviceList) {
				brandLists.add(service.getBrandList());
			}
			session.put(Constant.S_BRAND_LISTS, brandLists);
		}
		if (!session.containsKey(Constant.S_NEW_LISTS)) {
			List<List<Hardware>> newLists = new LinkedList<List<Hardware>>();
			for(Service<?> service : serviceList) {
				newLists.add(service.getNewestList());
			}
			session.put(Constant.S_NEW_LISTS, newLists);
		}
		if (!session.containsKey(Constant.S_HOT_LISTS)) {
			List<List<Hardware>> hotLists = new LinkedList<List<Hardware>>();
			for(Service<?> service : serviceList) {
				hotLists.add(service.getHottestList());
			}
			session.put(Constant.S_HOT_LISTS, hotLists);
		}
		return SUCCESS;
	}

}
